<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="title">初装家居室内空气质量监测系统</h2>
      
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username"
            placeholder="请输入员工用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password_hash">
          <el-input 
            v-model="loginForm.password_hash"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <div class="button-group">
          <el-button type="primary" @click="handleLogin" :loading="loading">登录</el-button>
          <el-button @click="handleReset">取消</el-button>
        </div>

        <div class="extra-links">
          <el-link type="primary" @click="goToRegister">注册账号</el-link>
          <el-link type="primary" @click="goToForget">忘记密码</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/auth'

const router = useRouter()
const loading = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({
  username: '',
  password_hash: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password_hash: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        const res = await login(loginForm)
        if (res.code === 1) {
          localStorage.setItem('token', res.data.token)
          localStorage.setItem('userInfo', JSON.stringify(res.data))
          ElMessage.success('登录成功')
          router.push('/home')
        } else {
          ElMessage.error(res.message || '登录失败')
        }
      } catch (error) {
        ElMessage.error('登录失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
  })
}

const handleReset = () => {
  loginFormRef.value?.resetFields()
}

const goToRegister = () => {
  router.push('/register')
}

const goToForget = () => {
  router.push('/forget')
}
</script>

<style scoped>
.login-container {
  margin-left: -230px;
  width: 180vh;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 30px;
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
  font-weight: 500;
}

.button-group {
  margin-top: 30px;
  display: flex;
  justify-content: center;
  gap: 20px;
}

.button-group .el-button {
  width: 120px;
}

.extra-links {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  padding: 0 10px;
}

:deep(.el-input__wrapper) {
  background-color: #fff;
}

:deep(.el-input__inner) {
  height: 40px;
}

:deep(.el-button--primary) {
  background-color: #409EFF;
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}
</style> 